import 'package:flutter/material.dart';

/// @date 2020/08/13  17:22
/// @author 程志强
/// @des 出入滑动动画

class SlideTransitionX extends AnimatedWidget {
  SlideTransitionX(
      {@required Animation<double> position,
      this.transformHitTests = true,
      this.direction = AxisDirection.down,
      this.child})
      : assert(position != null),
        super(listenable: position) {
    switch (direction) {
      case AxisDirection.up:
        _tween = Tween(begin: Offset(0, 1), end: Offset(0, 0));
        break;
      case AxisDirection.right:
        _tween = Tween(begin: Offset(-1, 0), end: Offset(0, 0));
        break;
      case AxisDirection.down:
        _tween = Tween(begin: Offset(0, -1), end: Offset(0, 0));
        break;
      case AxisDirection.left:
        _tween = Tween(begin: Offset(1, 0), end: Offset(0, 0));
        break;
      default:
        break;
    }
  }

  Animation<double> get position => listenable;

  final bool transformHitTests;
  final Widget child;

  // 退场方向
  final AxisDirection direction;

  Tween<Offset> _tween;

  Widget build(BuildContext context) {
    Offset offset = _tween.evaluate(position);
    if (position.status == AnimationStatus.reverse) {
      switch (direction) {
        case AxisDirection.up:
        case AxisDirection.down:
          offset = Offset(offset.dx, -offset.dy);
          break;
        case AxisDirection.right:
        case AxisDirection.left:
          offset = Offset(-offset.dx, offset.dy);
          break;
        default:
          break;
      }
    }

    return FractionalTranslation(
      translation: offset,
      transformHitTests: transformHitTests,
      child: child,
    );
  }
}
